//
//  CSGXPreviewCell.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/10/28.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXPreviewCell.h"
#import "LKGXNumberModificationView.h"

#define kPrice 50.00

@interface CSGXPreviewCell () <NumberModificationViewDelegate>

@property(nonatomic,weak) UILabel *lblIndex;
@property (nonatomic,weak) UILabel * lblName;
@property(nonatomic,weak) UILabel *lblOtherInfos;
/// 大单位
@property(nonatomic,weak) LKGXNumberModificationView *bigTypeView;
/// 小单位
@property(nonatomic,weak) LKGXNumberModificationView *smallTypeView;
@property(nonatomic,weak) UILabel *lblPriceInfo;
@property(nonatomic,weak) UILabel *lblTotalPrice;

///修改的单位索引
@property(nonatomic, assign) NSInteger currentCountModiTag;

@property (nonatomic, strong) NSArray<LKGXNumberModificationView *> * numModiArr;

@end

@implementation CSGXPreviewCell


#pragma mark - actions
- (void)btnClear_Click:(UIButton *)sender {
    // 清零
    self.bigTypeView.commodityCount = 0;
    self.smallTypeView.commodityCount = 0;
    self.lblTotalPrice.text = @"¥0.00";
    
    _salePreviewCellModel.sCount = @",";
    _salePreviewCellModel.sUnitMoney = @",";
    _salePreviewCellModel.sTotalMoney = 0;
    
    [self redistributePreviewCell];
    
#pragma mark call back
    if (self.callback_previewCell) {
        self.callback_previewCell();
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    //    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}




#pragma mark - methods

- (void)redistributePreviewCell {
    for (int i = 0; i < self.numModiArr.count; i++) {
        self.numModiArr[i].commodityCount = 0;
    }
}

- (void)distributePreview {
    
    NSArray * unitArr = [self.salePreviewCellModel.sUnit componentsSeparatedByString:@","];
    
    NSArray<NSString *> * unitCountArr = [self.salePreviewCellModel.sCount componentsSeparatedByString:@","];
    
    NSArray<NSString *> * priceArr = [self.salePreviewCellModel.sPrice componentsSeparatedByString:@","];
    
    //”1*10 2008-01-01 库存: **箱**包“
    NSString * otherInfoStr = @"1*10 2008-01-01 库存: **箱**包";
    
    self.lblName.text = self.salePreviewCellModel.sName;
    self.lblIndex.text = [@(self.cellIndex + 1) gx_toString];
    self.lblOtherInfos.text = otherInfoStr;
    
    self.bigTypeView.dataNumModiDict = @{@"unitType": unitArr[0], @"unitCount":unitCountArr[0]};
    NSMutableArray<LKGXNumberModificationView *> * tempNumModiArrM = [NSMutableArray<LKGXNumberModificationView *> array];
    for (int i = 1; i < unitArr.count; i++) {
        // 1. 大单位已经创建，再创建其他小单位。
        // 2. 布局
        LKGXNumberModificationView *numModiView = [LKGXNumberModificationView modificationView];
        [self.contentView addSubview:numModiView];
        
        numModiView.countDelegate = self;
        numModiView.dataNumModiDict = @{@"unitType": unitArr[i], @"unitCount":unitCountArr[i]};
        
        [tempNumModiArrM addObject:numModiView];
    }
    self.numModiArr = tempNumModiArrM.copy;
    
    // 布局
    for (int i = 0; i < tempNumModiArrM.count; i ++) {
        if (i == 0) {
            [tempNumModiArrM[i] mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.top.equalTo(self.bigTypeView);
                make.leading.equalTo(self.bigTypeView.mas_trailing).offset(5);
            }];
        }else {
            [tempNumModiArrM[i] mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.top.equalTo(self.bigTypeView);
                make.leading.equalTo(tempNumModiArrM[i-1].mas_trailing).offset(5);
            }];
        }
    }
    
//    self.smallTypeView.dataNumModiDict = @{@"unitType": unitArr[1], @"unitCount": unitCountArr[1]};
    
    NSString *priceStr = [NSString stringWithFormat:@"￥%@/%@ %@/%@", priceArr[0], unitArr[0], priceArr[1], unitArr[1]];
    NSMutableAttributedString *priceAttrStr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [priceAttrStr setAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} range:NSMakeRange(0, 1)];
    
    self.lblPriceInfo.attributedText = priceAttrStr.copy;
    self.lblTotalPrice.text = [NSString stringWithFormat:@"%.2f元", self.salePreviewCellModel.sTotalMoney];
}

#pragma mark - delegate
- (void)numberModificationView:(LKGXNumberModificationView *)numberModificationView commodityCount:(NSInteger)commodityCount {
    //根据单位unit修改Model对应的数量和价格
    self.currentCountModiTag = numberModificationView.tag; //修改的哪个单位的数量
    
    NSMutableArray<NSString *> *tempUnitCountArrM = [NSMutableArray<NSString *> arrayWithArray:[self.salePreviewCellModel.sCount componentsSeparatedByString:@","] ];
    if (numberModificationView == _bigTypeView) {
        // 大单位
        tempUnitCountArrM[0] = [@(numberModificationView.commodityCount) gx_toString];
    }else {
        // 小单位
        tempUnitCountArrM[1] = [@(numberModificationView.commodityCount) gx_toString];
    }
    self.salePreviewCellModel.sCount = [NSString stringWithFormat:@"%@,%@", tempUnitCountArrM[0], tempUnitCountArrM[1]];
    
    // 修改对应的金额
    NSArray * priceArr = [self.salePreviewCellModel.sPrice componentsSeparatedByString:@","];
    
    NSString * unitMoneyStr = @"";
    CGFloat totalMoneyF = 0;
    for (int i = 0; i < priceArr.count; i++) {
        CGFloat unitMoneyF = [priceArr[i] floatValue] * [tempUnitCountArrM[i] integerValue];
        
        totalMoneyF += unitMoneyF;
        unitMoneyStr = [unitMoneyStr stringByAppendingFormat:@"%@,", [@(unitMoneyF) gx_toString]];
    }
    
    self.salePreviewCellModel.sUnitMoney = [unitMoneyStr subStringFrontOfLastIndex:1];
    self.salePreviewCellModel.sTotalMoney = totalMoneyF;
    
    self.lblTotalPrice.text = [NSString stringWithFormat:@"%.2f元", totalMoneyF];
    
#pragma mark call back
    if (self.callback_previewCell) {
        self.callback_previewCell();
    }
}


#pragma mark - setupUI

- (void) setupUI {
    CGFloat fontSizeF = 12;
    CGFloat marginTopF = 5;
    
    //lblIndex
    UILabel * lblIndex = [UILabel gx_labelWithText:@"66" fontSize:15 color:[UIColor orangeColor]];
    [self.contentView addSubview:lblIndex];
    lblIndex.textAlignment = NSTextAlignmentCenter;
    
    [lblIndex mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(self.contentView);
        make.width.offset(20);
    }];
    
    //lblName
    UILabel * lblName = [UILabel gx_labelWithText:@"综合商品" fontSize:fontSizeF color:[UIColor blackColor]];
    [self.contentView addSubview:lblName];
    //    lblName.backgroundColor = [UIColor greenColor];
    
    [lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(2);
        make.leading.equalTo(lblIndex.mas_trailing).mas_offset(marginTopF);
    }];
    
    //lblOtherInfos
    UILabel * lblOtherInfos = [UILabel gx_labelWithText:@"otherInfos" fontSize:fontSizeF color:[UIColor grayColor]];
    [self.contentView addSubview:lblOtherInfos];
    //    lblOtherInfos.backgroundColor = [UIColor yellowColor];
    
    [lblOtherInfos mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblName.mas_bottom).offset(5);
        make.leading.equalTo(lblName);
        make.trailing.equalTo(self.contentView.mas_trailing).mas_offset(-marginTopF);
    }];
    
    // bigTypeView //暂时创建一个
    LKGXNumberModificationView * bigTypeView = [LKGXNumberModificationView modificationView];
    [self.contentView addSubview:bigTypeView];
    bigTypeView.countDelegate = self;
    
    [bigTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblOtherInfos.mas_bottom).mas_offset(marginTopF);
        make.leading.equalTo(lblIndex.mas_trailing);
        make.size.mas_offset(CGSizeMake(120, 30));
    }];
    
    //lblPriceInfo
    UILabel * lblPriceInfo = [UILabel gx_labelWithText:@"￥priceInfopriceInfopriceInfo" fontSize:fontSizeF color:[UIColor grayColor]];
    [self.contentView addSubview:lblPriceInfo];
    
    [lblPriceInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bigTypeView.mas_bottom).mas_offset(marginTopF);
        make.leading.equalTo(lblOtherInfos);
        make.bottom.equalTo(self.contentView).offset(-3);
    }];
    
    //lblTotalPrice
    UILabel * lblTotalPrice = [UILabel gx_labelWithText:@"totalPrice" fontSize:fontSizeF color:[UIColor orangeColor]];
    [self.contentView addSubview:lblTotalPrice];
    
    [lblTotalPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblPriceInfo);
        make.trailing.equalTo(self.contentView).mas_offset(-2 * marginTopF);
        
    }];
    
    // btnClear
    UIButton * btnClear = [UIButton gx_textButton:@"清零" fontSize:17 normalColor:kColorBar selectedColor:nil];
    [self.contentView addSubview:btnClear];
    [btnClear addTarget:self action:@selector(btnClear_Click:) forControlEvents:UIControlEventTouchUpInside];
    
    [btnClear.titleLabel setFont:[UIFont boldSystemFontOfSize:17]];
    
    [btnClear mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(0);
        make.trailing.equalTo(self.contentView).offset(-2);
    }];
    
    [self layoutIfNeeded];
    
    self.lblIndex = lblIndex;
    self.lblName = lblName;
    self.lblOtherInfos = lblOtherInfos;
    self.bigTypeView = bigTypeView;
//    self.smallTypeView = smallTypeView;
    self.lblPriceInfo = lblPriceInfo;
    self.lblTotalPrice = lblTotalPrice;
}

#pragma mark - setter & getter

- (void)setSalePreviewCellModel:(TSaleInfoModel *)salePreviewCellModel {
    _salePreviewCellModel = salePreviewCellModel;
    // 分发数据 distribute
    [self distributePreview];
}

- (void)setCellIndex:(NSInteger)cellIndex {
    _cellIndex = cellIndex;
    
    self.lblIndex.text = [@(cellIndex + 1) gx_toString];
}

@end
